iT邦幫忙

2025 iThome 鐵人賽

DAY 13
0
Cloud Native

《30 天 Cloud Native:AI 行程規劃後端開發》系列 第 13

Day13 - 從 Prompt 到 JSON:Gemini API 行程規劃實作與錯誤排查全紀錄

  • 分享至 

  • xImage
  •  

今天先來當個魔法師,詠唱 Prompt 讓 Gemini 可以幫忙規劃行程並依照需要的格式回覆:

You are an itinerary generator. Please responde with a detailed timetable. The output must follow the schema below:

{
	activities: [
		{
			type: "sightseeing, restaurant, shopping, accommodation, freeTime, transport, other",
			location: "string",
			startTime: "rfc3339",
			duration: "minutes",
			endTime: "rfc3339",
			transportType: "train, highSpeedTrain, flight, bus, taxi, bike, walk, car, boat, motorcycle, other",
			note: "string",
			childActivities: [
				{
					name: "activity name",
					duration: "minutes",
				}
			]
		}
	]
}

Instructions:
- Output must be valid JSON.
- No extra commentary or formatting.
- Do not include any explanations, markdown, or extra text.
- Use rfc3339 time format.
- All keys must be present and correctly spelled.

傳送使用者資料給 Gemini

收到使用者的資料後將資訊整合傳給 Gemini :

Please plan a {days}-days trip starting from {start_date} in {locations}. Use {language} for all value data.

設定參數

參數分別設定為:

locations = ["日月潭"]
locations = ','.join(locations)
start_date = '2025-09-10'
language = 'Chinese Traditional'
days = 2

Gemini 回覆結果

獲得了以下結果:

{
    "activities": [
        {
            "type": "transport",
            "location": "日月潭",
            "startTime": "2025-09-10T09:00:00+08:00",
            "duration": 60,
            "endTime": "2025-09-10T10:00:00+08:00",
            "transportType": "other",
            "note": "抵達日月潭,前往住宿地點辦理入住手續。",
            "childActivities": []
        },
        {
            "type": "accommodation",
            "location": "日月潭",
            "startTime": "2025-09-10T10:00:00+08:00",
            "duration": 60,
            "endTime": "2025-09-10T11:00:00+08:00",
            "transportType": null,
            "note": "於日月潭湖畔飯店辦理入住手續。",
            "childActivities": []
        },
        {
            "type": "sightseeing",
            "location": "日月潭水社碼頭",
            "startTime": "2025-09-10T11:00:00+08:00",
            "duration": 90,
            "endTime": "2025-09-10T12:30:00+08:00",
            "transportType": "boat",
            "note": "搭乘遊艇遊覽日月潭風光,途經拉魯島。",
            "childActivities": [
                {
                    "name": "日月潭遊湖",
                    "duration": 90
                }
            ]
        },
        {
            "type": "restaurant",
            "location": "伊達邵碼頭",
            "startTime": "2025-09-10T12:30:00+08:00",
            "duration": 60,
            "endTime": "2025-09-10T13:30:00+08:00",
            "transportType": "walk",
            "note": "於伊達邵品嚐當地特色小吃及原住民風味餐。",
            "childActivities": []
        },
        {
            "type": "sightseeing",
            "location": "伊達邵部落",
            "startTime": "2025-09-10T13:30:00+08:00",
            "duration": 90,
            "endTime": "2025-09-10T15:00:00+08:00",
            "transportType": "walk",
            "note": "逛伊達邵商店街,體驗邵族文化。",
            "childActivities": [
                {
                    "name": "逛伊達邵商店街",
                    "duration": 60
                },
                {
                    "name": "邵族文化體驗",
                    "duration": 30
                }
            ]
        },
        {
            "type": "sightseeing",
            "location": "文武廟",
            "startTime": "2025-09-10T15:30:00+08:00",
            "duration": 90,
            "endTime": "2025-09-10T17:00:00+08:00",
            "transportType": "bus",
            "note": "參觀香火鼎盛的文武廟,欣賞壯麗的建築與湖景。",
            "childActivities": []
        },
        {
            "type": "restaurant",
            "location": "日月潭水社碼頭周邊",
            "startTime": "2025-09-10T18:30:00+08:00",
            "duration": 90,
            "endTime": "2025-09-10T20:00:00+08:00",
            "transportType": "walk",
            "note": "享用晚餐,可選擇當地餐廳或飯店內用餐。",
            "childActivities": []
        },
        {
            "type": "freeTime",
            "location": "日月潭",
            "startTime": "2025-09-10T20:00:00+08:00",
            "duration": 120,
            "endTime": "2025-09-10T22:00:00+08:00",
            "transportType": null,
            "note": "自由活動時間,可於湖畔散步或飯店設施。",
            "childActivities": []
        },
        {
            "type": "accommodation",
            "location": "日月潭",
            "startTime": "2025-09-10T22:00:00+08:00",
            "duration": 600,
            "endTime": "2025-09-11T08:00:00+08:00",
            "transportType": null,
            "note": "於日月潭湖畔飯店住宿。",
            "childActivities": []
        },
        {
            "type": "sightseeing",
            "location": "玄光寺與玄奘寺",
            "startTime": "2025-09-11T09:00:00+08:00",
            "duration": 120,
            "endTime": "2025-09-11T11:00:00+08:00",
            "transportType": "bus",
            "note": "參觀玄光寺,品嚐阿嬤香菇茶葉蛋,後前往玄奘寺。",
            "childActivities": [
                {
                    "name": "玄光寺",
                    "duration": 60
                },
                {
                    "name": "玄奘寺",
                    "duration": 60
                }
            ]
        },
        {
            "type": "sightseeing",
            "location": "慈恩塔",
            "startTime": "2025-09-11T11:00:00+08:00",
            "duration": 60,
            "endTime": "2025-09-11T12:00:00+08:00",
            "transportType": "bus",
            "note": "攀登慈恩塔,俯瞰日月潭全景。",
            "childActivities": []
        },
        {
            "type": "restaurant",
            "location": "水社碼頭周邊",
            "startTime": "2025-09-11T12:30:00+08:00",
            "duration": 60,
            "endTime": "2025-09-11T13:30:00+08:00",
            "transportType": "walk",
            "note": "享用午餐,可選擇當地餐廳。",
            "childActivities": []
        },
        {
            "type": "shopping",
            "location": "日月潭水社碼頭商店街",
            "startTime": "2025-09-11T13:30:00+08:00",
            "duration": 90,
            "endTime": "2025-09-11T15:00:00+08:00",
            "transportType": "walk",
            "note": "購買日月潭紀念品與特產,如紅茶、糕點等。",
            "childActivities": []
        },
        {
            "type": "transport",
            "location": "日月潭",
            "startTime": "2025-09-11T15:00:00+08:00",
            "duration": 120,
            "endTime": "2025-09-11T17:00:00+08:00",
            "transportType": "other",
            "note": "離開日月潭,返回出發地。",
            "childActivities": []
        }
    ]
}

看起來還不錯,接下來整合一下架構,用 Cloud Run Service 架一個 API server ,然後在收到請求和資料後呼叫 Vertex AI Agent Engine 產生 JSON ,最後回傳給使用者。

問題排查

很快地在部屬寫好的 Agent 時出現了問題,當我測試遠端 Agent 請他產生行程時什麼都沒有出現,所以到 Logs Explorer 找問題:

[175]     ERROR:    1 validation error for InvocationContext
agent
  Input should be a valid dictionary or instance of BaseAgent [type=model_type, input_value=<vertexai.preview.reasoni...bject at 0x7f77a61289d0>, input_type=AdkApp]
    For further information visit https://errors.pydantic.dev/2.11/v/model_type
[175]     ERROR:    Please refer to the troubleshooting guide (https://cloud.google.com/vertex-ai/generative-ai/docs/agent-engine/troubleshooting/use) to debug and fix the error.
[175]     ERROR:    Exception in ASGI application
  + Exception Group Traceback (most recent call last):
  |   File "/home/appuser/.cache/pypoetry/virtualenvs/reasoning-engine-assembly-service-MATOk_fk-py3.10/lib/python3.10/site-packages/starlette/_utils.py", line 76, in collapse_excgroups
...

後面有一堆的 Trace 但是都不是我的程式碼,看起來是 Vertex AI Agent Engine 部屬時用到的內部服務壞掉了,我不斷的修改程式碼用不同的方式呼叫,請 Gemini 分析發生了什麼問題,但結果都是在說輸入有問題之類的,不過輸入有問題的套件不是我這邊的扣,突然想到該不會是套件版本問題吧,於是找到 Cloud Build 的歷史紀錄,搜尋 google-cloud-aiplatform 發現版本是 1.111.0 ,本地的是 1.110.0

Identified the following requirements: {'google-cloud-aiplatform': '1.111.0', 'cloudpickle': '3.1.1', 'pydantic': '2.11.7'}

剛好之前測試時多建立了一個 Agent Engine ,把 AGENT_ID 換過去執行就能順利得到回覆:

{'sessions': []}
{'state': {}, 'userId': 'USER_ID', 'appName': '5626843114120413184', 'id': '5283721269219426304', 'events': [], 'lastUpdateTime': 1756306658.901467}
很抱歉,我無法協助您規劃日月潭的行程。我的功能僅限於查詢特定城市的天氣和時間。

所以遠端測試的程式碼沒有問題,有問題的是部屬的環境,興高采烈的指定好套件版本部屬出去後,問題還是存在,要截稿了,明天再繼續吧。


上一篇
Day12 - Agent 工具鏈實驗:多步驟任務與 Tool 呼叫測試
下一篇
Day14 - 從 ADK 到 Cloud Run:打造完整的 Vertex AI Agent 呼叫流程
系列文
《30 天 Cloud Native:AI 行程規劃後端開發》21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言